package com.lemon.secondhandplatform.broadcast;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo.State;

import com.lemon.secondhandplatform.BaseApplication;

/**
 * 网络状态监听广播
 * 主要功能：用于监听网络状态的改变
 */
public class NetworkChangeReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        State wifiState;
        State mobileState;

        ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        wifiState = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI).getState();
        mobileState = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE).getState();

        if (wifiState == State.CONNECTED) {//判断是否连接wifi
            BaseApplication.wifi = true;
        } else {
            BaseApplication.wifi = false;
        }

        if (mobileState == State.CONNECTED) {//判断是否连接手机网络
            BaseApplication.mobile = true;
        } else {
            BaseApplication.mobile = false;
        }

        if (wifiState == State.CONNECTED || mobileState == State.CONNECTED) {//判断是否连网
            BaseApplication.network = true;
        } else {
            BaseApplication.network = false;
        }
    }
}
